package uast.bpdex.actions.addUL1;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import java.util.regex.*;
import uast.bpdex.forms.addUL1;
import uast.bpdex.BaseDatos.*;
import uast.bpdex.Comunidad;
import uast.bpdex.AsoCivil;

/**
 *
 *Clase utilizada para manipular las acciones sobre el formulario addUL1
 */
public class addUL1Action extends org.apache.struts.action.Action {
     
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String tipo, target = "failAddU";
        String[] valores;
        DataBase db;
        Pattern p;
        Matcher m;
        Comunidad com;
        AsoCivil ac;
        int i;

        if(form == null)
          return mapping.findForward(target);
        
        addUL1 form0 = (addUL1)form;
        
        tipo = form0.getTipo();
        if(tipo.equals("com")){
            valores = new String[16];
        }else if(tipo.equals("ac")){
            valores = new String[15];
        }else{
           return mapping.findForward(target);           
        }
        valores[0] = form0.getUsr();
        valores[1] = form0.getPssw();
        valores[2] = form0.getNombre();
        valores[3] = form0.getUg();
        valores[4] = form0.getRif();
        valores[5] = form0.getEmail();
        valores[6] = form0.getTlf();
        valores[7] = form0.getEstado();
        valores[8] = form0.getMunicipio();
        valores[9] = form0.getParroquia();
        valores[10] = form0.getSector();
        valores[11] = form0.getMiembros();
        valores[12] = form0.getMision();
        valores[13] = form0.getPregunta();
        valores[14] = form0.getRespuesta();        
        if(tipo.equals("com")){
          valores[15] = form0.getNumM();

          //Se valida el numero de miembros
          p = Pattern.compile("\\d+");
          m = p.matcher(valores[15]);
          if(!m.matches())
            return mapping.findForward(target);           
        }
        
        for(i = 0; i < valores.length; ++ i)
            if(valores[i].equals(""))
              return mapping.findForward(target);            
 
        //Validacion de parametros recibidos
        //Se valida el rif
        p = Pattern.compile("[j|J|]-[0-9]+-([0-9]|[a-zA-Z])");
        m = p.matcher(valores[4]);
        if(!m.matches())
          return mapping.findForward(target);

        //Se valida el email
        p = Pattern.compile(".+@.+\\..+");
        m = p.matcher(valores[5]);
        if(!m.matches())
          return mapping.findForward(target);
                
        try{
            db = new DataBase();
            if(tipo.equals("com")){
              com = new Comunidad(valores[0],valores[1],valores[2],valores[3],valores[4],valores[5],valores[6],valores[7],valores[8],valores[9],valores[10],valores[11],valores[12], valores[15], valores[13], valores[14]);
              db.agregarComunidad(com);
            }else if(tipo.equals("ac")){
              ac = new AsoCivil(valores[0],valores[1],valores[2],valores[3],valores[4],valores[5],valores[6],valores[7],valores[8],valores[9],valores[10],valores[11],valores[12], valores[13], valores[14]);
              db.agregarAsoCivil(ac);
            }
            target = "succAddU";
        }catch(Exception ex0){
           return mapping.findForward(target);
        }
        return mapping.findForward(target);
    }
}
